<template>
  <div class="form">
    <el-form label-position="top" label-width="80px" :model="menuForm">
      <el-form-item label="菜单名称">
        <el-input v-model="menuForm.title"></el-input>
      </el-form-item>
      <el-form-item label="父菜单">
        <div class="block">
          <span>{{parentMenuName}}</span>&nbsp;&nbsp;&nbsp;&nbsp;
          <el-popover
            placement="right"
            width="200"
            trigger="click">
            <el-cascader
                v-model="selectedParentMenuId"
                :options="parentMenu"
                :props="parentMenuProps"
                placeholder="根节点"
                @change="selectParentMenu"
                clearable></el-cascader>
                <el-button type="text" slot="reference">修改</el-button>
          </el-popover>
        </div>
      </el-form-item>
      <el-form-item label="路径">
        <el-input v-model="menuForm.path"></el-input>
      </el-form-item>
      <el-form-item label="代号">
        <el-input v-model="menuForm.code"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <el-input v-model="menuForm.icon"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="menuForm.seq"  :min="1"  label="排序" controls-position="right"></el-input-number>
      </el-form-item>
      <el-form-item label="有无子菜单">
        <el-switch
          v-model="menuForm.hasChildren"
          active-value=1
          inactive-value=0>
        </el-switch>
      </el-form-item>
      <el-form-item label="是否隐藏">
        <el-switch
          v-model="menuForm.hidden"
          active-value=1
          inactive-value=0>
        </el-switch>
      </el-form-item>
      <div style="text-align: right;">
        <el-form-item>
          <el-button type="primary" @click="onUpdate">保存</el-button>
          <el-button @click="onClose">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import api from '@/api'

export default {
  name: 'menu-edit',
  props: {
    menuId: {
      type: Number
    }
  },
  data () {
    return {
      menuForm: {
      },
      parentMenuName: '',
      parentMenu: [],
      parentMenuProps: {
        checkStrictly: true,
        lazy: true,
        lazyLoad (node, resolve) {
          if (node.level === 0) {

          } else {
            const param = { parentId: node.value }
            api.sys_menu_list(param).then(res => {
              const nodes = res.map(item => {
                const node = {}
                node.value = item.id
                node.label = item.title
                return node
              })
              resolve(nodes)
            })
          }
        }
      },
      selectedParentMenuId: []
    }
  },
  mounted () {
    this.getMenuById()
    this.getMenuByParentId(0)
  },
  methods: {
    onUpdate () {
      var param = this.menuForm
      api.sysMenuUpdateById(param).then(res => {
        this.$message({ type: 'info', message: '已更新' })
        this.onClose()
        this.$emit('refresh')
      })
    },
    onUpdateParentMenu () {
    },
    selectParentMenu (node) {
      if (node.length === 0) {
        this.menuForm.parentId = 0
      } else {
        this.menuForm.parentId = node[0]
      }
      this.getParentMenuById()
    },
    getMenuById () {
      if (this.menuId === 0 || !this.menuId) {
        return
      }
      var param = { id: this.menuId }
      api.sysMenuFindById(param).then(res => {
        this.menuForm = res
        this.getParentMenuById()
      })
    },
    getParentMenuById () {
      var param = { id: this.menuForm.parentId }
      if (this.menuForm.parentId === 0) {
        this.parentMenuName = '根节点'
        return
      }
      api.sysMenuFindById(param).then(res => {
        this.parentMenuName = res.title
      })
    },
    getMenuByParentId (id) {
      var param = { parentId: id }
      api.sys_menu_list(param).then(res => {
        this.parentMenu = res.map(item => {
          const node = {}
          node.value = item.id
          node.label = item.title
          node.children = item.children
          return node
        })
      })
    },
    onClose () {
      this.$emit('closeDrawer')
    }
  }
}
</script>

<style lang="scss">
  .form {
    padding: 10px;
  }
</style>
